package com.suda.sudaexamsystem.controller;

import com.suda.sudaexamsystem.dto.ExamPaperDto;
import com.suda.sudaexamsystem.dto.QuestionDto;
import com.suda.sudaexamsystem.pojo.ExamPaper;
import com.suda.sudaexamsystem.pojo.Question;
import com.suda.sudaexamsystem.service.ExamPaperService;
import com.suda.sudaexamsystem.util.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/exam")
@Slf4j
public class ExamPaperController {
    @Autowired
    private ExamPaperService examPaperService;

    // 添加试卷 传递的参数json和实体对象的属性名一致 但是是Snake风格的
    // 属性名questionList  snake风格就是question_list
    // 不需要手动计算试卷总分
    @PostMapping(value = "/addExam", produces = {"application/json;charset=UTF-8"})
    public AjaxResult addExamPaper(@RequestBody ExamPaper examPaper) {
        BigDecimal score = new BigDecimal(0);
        for (Question question : examPaper.getQuestionList()) {
            question.setExamPaper(examPaper);
            score = score.add(question.getScore());
        }
        examPaper.setScore(score);
        examPaperService.addExamPaper(examPaper);
        return AjaxResult.success();
    }

    // 删除试卷
    @PostMapping(value = "/delete/{examId}", produces = {"application/json;charset=UTF-8"})
    public AjaxResult deleteExamPaper(@PathVariable("examId") int examId) {
        log.info(examId + "");
        examPaperService.deleteById(examId);
        return AjaxResult.success();
    }

    // 查询试卷
    /*
    {"msg":"操作成功","code":200,"data":{"id":1,"name":"软件项目管理","score":5.00,"clazz":{"id":1,"name":"神雕侠侣","owner":{"id":1,"name":"admin","login_name":"段智兴","sex":"男"}},
    "question_dto_list":[{"id":1,"order_id":1,"name":"如果项目失败，负主要责任的是（）","type":{"id":1,"name":"选择题"},"score":1.00,"option_a":"决策层","option_b":"项目发起人","option_c":"项目经理","option_d":"管理层","answer":"C"},{"id":2,"order_id":2,"name":"在项目管理中，管理层扮演的角色是（）","type":{"id":1,"name":"选择题"},"score":1.00,
    "option_a":"财务监控者","option_b":"独裁者","option_c":"插手实施工作者","option_d":"支持者","answer":"D"},{"id":3,"order_id":3,"name":"下列哪个选项时我们做报告时最应该担心的（）","type":{"id":1,"name":"选择题"},"score":1.00,"option_a":"听众","option_b":"报告内容与信息含量","option_c":"提议实施的技术","option_d":"报告时间长短","answer":"B"},{"id":4,"order_id":4,
    "name":"如果你是IT项目经理，和客户经理们谈话时应该重点讲述的是（）","type":{"id":1,"name":"选择题"},"score":1.00,"option_a":"项目结果","option_b":"项目经理在公司内的业绩","option_c":"技术速度","option_d":"工作效率","answer":"A"},{"id":5,"order_id":5,"name":"项目某个阶段结束的标志是（）","type":{"id":1,"name":"选择题"},"score":1.00,"option_a":"WBS内确定的日期",
    "option_b":"项目团队的派对","option_c":"达到指定的里程碑","option_d":"最终的交付成果","answer":"C"}]}}
     */
    @GetMapping(value = "/search/{examId}", produces = {"application/json;charset=UTF-8"})
    public AjaxResult findExamPaper(@PathVariable("examId") int examId) {
        ExamPaper examPaper = examPaperService.findById(examId);
        ExamPaperDto examPaperDto = new ExamPaperDto();
        examPaperDto.setClazz(examPaper.getClazz().toDto());
        examPaperDto.getClazz().setOwner(examPaper.getClazz().getOwner().toDto());
        examPaperDto.setId(examPaper.getId());
        examPaperDto.setName(examPaper.getName());
        examPaperDto.setScore(examPaper.getScore());
        List<QuestionDto> questionDtoList = examPaperDto.getQuestionDtoList();
        for (Question question : examPaper.getQuestionList()) {
            QuestionDto questionDto = new QuestionDto();
            questionDto.setId(question.getId());
            questionDto.setName(question.getName());
            questionDto.setScore(question.getScore());
            questionDto.setType(question.getType());
            questionDto.setAnswer(question.getAnswer());
            questionDto.setOptionA(question.getOptionA());
            questionDto.setOptionB(question.getOptionB());
            questionDto.setOptionC(question.getOptionC());
            questionDto.setOptionD(question.getOptionD());
            questionDto.setOrderId(question.getOrderId());
            questionDtoList.add(questionDto);
        }
        return AjaxResult.success(examPaperDto);
    }

    // 查询班级所有试卷
    @GetMapping(value = "/searchByClazz/{clazzId}", produces = {"application/json;charset=UTF-8"})
    public AjaxResult findExamPapers(@PathVariable("clazzId") int clazzId) {
        List<ExamPaper> examPaperList = examPaperService.findByClazzId(clazzId);
        List<ExamPaperDto> examPaperDtoList = new ArrayList<>();
        for (ExamPaper examPaper : examPaperList) {
            ExamPaperDto examPaperDto = new ExamPaperDto();
            examPaperDto.setClazz(examPaper.getClazz().toDto());
            examPaperDto.getClazz().setOwner(examPaper.getClazz().getOwner().toDto());
            examPaperDto.setId(examPaper.getId());
            examPaperDto.setName(examPaper.getName());
            examPaperDto.setScore(examPaper.getScore());
            List<QuestionDto> questionDtoList = examPaperDto.getQuestionDtoList();
            for (Question question : examPaper.getQuestionList()) {
                QuestionDto questionDto = new QuestionDto();
                questionDto.setId(question.getId());
                questionDto.setName(question.getName());
                questionDto.setScore(question.getScore());
                questionDto.setType(question.getType());
                questionDto.setAnswer(question.getAnswer());
                questionDto.setOptionA(question.getOptionA());
                questionDto.setOptionB(question.getOptionB());
                questionDto.setOptionC(question.getOptionC());
                questionDto.setOptionD(question.getOptionD());
                questionDto.setOrderId(question.getOrderId());
                questionDtoList.add(questionDto);
            }
            examPaperDtoList.add(examPaperDto);
        }
        return AjaxResult.success(examPaperDtoList);
    }
}
